package com.software.sqlite.api.app.v1.controller;

import com.software.sqlite.api.admin.v1.controller.BaseController;
import com.software.sqlite.api.app.v1.service.impl.MovieV1Service;
import com.software.sqlite.api.app.v1.vo.AppV1Result;
import com.software.sqlite.common.CommonContant;
import com.software.sqlite.entity.*;
import com.software.sqlite.service.impl.*;
import com.software.sqlite.utils.CommonUtils;
import com.software.sqlite.utils.DateUtils;
import com.software.sqlite.utils.HutoolCacheUtils;
import com.software.sqlite.utils.VerifyCodeUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.*;
import java.math.BigDecimal;
import java.util.Date;


@Api(value = "APP端-影视v1接口", tags = "APP端-影视v1接口")
@RestController
@Slf4j
@RequestMapping("/movie/v1/")
@CrossOrigin
public class MovieV1Controller extends BaseController {


    @Resource
    private MovieV1Service movieV1Service;
    @Resource
    private HttpServletRequest request;
    @Value("${weixin.app.appId}")
    private String appId;
    @Value("${weixin.app.appSecret}")
    private String appSecret;


    @ApiOperation(value = "个人注册", notes = "个人注册")
    @GetMapping("/register")
    public AppV1Result register(String userName,
                                String password,
                                String mobile,
                                String idCard,
                                String verifyCode,
                                Long shipId,String uuid){
        return movieV1Service.register(userName, password,mobile,idCard,verifyCode,shipId,uuid);
    }

    @GetMapping("/verifyCode")
    public void verifyCode(String type,String uuid,HttpServletRequest request, HttpServletResponse response) {
        try {
            OutputStream outputStream=response.getOutputStream();
            int w = 200, h = 80;
            String code = VerifyCodeUtils.generateVerifyCode(4);
            if(CommonUtils.isBlank(type)){
                type="login";
            }
            String key=type+"_verifyCode_"+uuid;
            HutoolCacheUtils.put(key,code);
            String getCode=CommonUtils.parseStringData(HutoolCacheUtils.get(key),"verifyCodeError");
            log.info(key+"："+getCode);
            VerifyCodeUtils.outputImage(w, h, outputStream, code);
            outputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    @ApiOperation(value = "个人登录", notes = "登录")
    @GetMapping("/login")
    public AppV1Result login(String userName,
                             String password,
                             String verifyCode,
                             String uuid){
        return movieV1Service.login(userName,password,verifyCode,uuid);
    }

    @ApiOperation(value = "加载个人信息", notes = "加载个人信息")
    @GetMapping("/myInfo")
    public AppV1Result myInfo(String uuid,String userToken){
        return movieV1Service.myInfo(uuid,userToken);
    }

    @ApiOperation(value = "党建影视列表", notes = "党建影视列表")
    @GetMapping("/dangjianList")
    public AppV1Result dangjianList(String keyWord,String uuid,String userToken){
        return movieV1Service.dangjianList(keyWord,uuid,userToken);
    }

    /**
     * 播放党建视频
     * @param movieId
     * @param response
     * @throws Exception
     */
    @GetMapping("/downloadDangjian")
    public void downloadDangjian(String movieId, HttpServletResponse response)throws Exception
    {
        movieV1Service.downloadDangjian(movieId,response);
    }
    /**
     * 播放党建视频
     * @param movieId
     * @param response
     * @throws Exception
     */
    @GetMapping("/showDangjian")
    public void showDangjian(String movieId, HttpServletResponse response)throws Exception
    {
        movieV1Service.showDangjian(movieId,response);
    }

    @ApiOperation(value = "综艺列表", notes = "综艺列表")
    @GetMapping("/zongyiList")
    public AppV1Result zongyiList(String keyWord,String uuid,String userToken){
        return movieV1Service.zongyiList(keyWord,uuid,userToken);
    }

    /**
     * 播放综艺视频
     * @param movieId
     * @param response
     * @throws Exception
     */
    @GetMapping("/downloadZongyi")
    public void downloadZongyi(String movieId, HttpServletResponse response)throws Exception
    {
        movieV1Service.downloadZongyi(movieId,response);
    }
    /**
     * 播放综艺视频
     * @param movieId
     * @param response
     * @throws Exception
     */
    @GetMapping("/showZongyi")
    public void showZongyi(String movieId, HttpServletResponse response)throws Exception
    {
        movieV1Service.showZongyi(movieId,response);
    }

    @ApiOperation(value = "影视列表", notes = "影视列表")
    @GetMapping("/movieList")
    public AppV1Result movieList(String keyWord,String uuid,String userToken,boolean needLogin){
        return movieV1Service.movieList(keyWord,uuid,userToken,needLogin);
    }

    @ApiOperation(value = "按分类-影视列表", notes = "按分类-影视列表")
    @GetMapping("/movieTypeList")
    public AppV1Result movieTypeList(String keyWord,String uuid,String userToken){
        return movieV1Service.movieTypeList(keyWord,uuid,userToken);
    }

    @ApiOperation(value = "影视列表", notes = "影视列表")
    @GetMapping("/myMovie")
    public AppV1Result myMovie(String keyWord,String uuid,String userToken,String movieId){
        return movieV1Service.myMovie(keyWord,uuid,userToken,movieId);
    }

    @RequestMapping("/showMovieImg")
    public void showImageByServer(String filePath,HttpServletRequest request, HttpServletResponse response){
        movieV1Service.showMovieImg(filePath,request,response);
    }

    @RequestMapping("/showMovieImgBase64")
    public AppV1Result showMovieImgBase64(String filePath,HttpServletRequest request, HttpServletResponse response){
        return movieV1Service.showMovieImgBase64(filePath,request,response);
    }


    @ApiOperation(value = "船舶列表", notes = "船舶列表")
    @GetMapping("/shipList")
    public AppV1Result shipList(String keyWord,String uuid,String userToken,String movieId){
        return movieV1Service.myMovie(keyWord,uuid,userToken,movieId);
    }

    @ApiOperation(value = "我的账单", notes = "我的账单")
    @GetMapping("/myBill")
    public AppV1Result myBill(String keyWord,String uuid,String userToken){
        return movieV1Service.myBill(keyWord,uuid,userToken);
    }

    @ApiOperation(value = "我的收藏", notes = "我的收藏")
    @GetMapping("/myCollect")
    public AppV1Result myCollect(String keyWord,String uuid,String userToken){
        return movieV1Service.myCollect(keyWord,uuid,userToken);
    }

    @ApiOperation(value = "保存我的收藏", notes = "保存我的收藏")
    @GetMapping("/saveMyCollect")
    public AppV1Result saveMyCollect(String keyWord,String uuid,String userToken){
        return movieV1Service.saveMyCollect(keyWord,uuid,userToken);
    }

    @ApiOperation(value = "观看记录", notes = "观看记录")
    @GetMapping("/myWatch")
    public AppV1Result myWatch(String keyWord,String uuid,String userToken){
        return movieV1Service.myWatch(keyWord,uuid,userToken);
    }

    @ApiOperation(value = "保存我的观看记录", notes = "保存我的观看记录")
    @GetMapping("/saveMyWatch")
    public AppV1Result saveMyWatch(String keyWord,String uuid,String userToken){
        return movieV1Service.saveMyWatch(keyWord,uuid,userToken);
    }

    @ApiOperation(value = "已购买项目", notes = "已购买项目")
    @GetMapping("/myBuy")
    public AppV1Result myBuy(String keyWord,String uuid,String userToken){
        return movieV1Service.myBuy(keyWord,uuid,userToken);
    }

    @ApiOperation(value = "我的喜欢", notes = "我的喜欢")
    @GetMapping("/myLike")
    public AppV1Result myLike(String keyWord,String uuid,String userToken){
        return movieV1Service.myLike(keyWord,uuid,userToken);
    }

    @ApiOperation(value = "保存我喜欢的影片", notes = "保存我喜欢的影片")
    @GetMapping("/saveMyLike")
    public AppV1Result saveMyLike(String keyWord,String uuid,String userToken){
        return movieV1Service.saveMyLike(keyWord,uuid,userToken);
    }

    @ApiOperation(value = "钱包充值", notes = "钱包充值")
    @GetMapping("/rechargeWallet")
    public AppV1Result rechargeWallet(String keyWord, String uuid, String userToken, BigDecimal amount){
        return movieV1Service.rechargeWallet(keyWord,uuid,userToken,amount);
    }


    @ApiOperation(value = "下单购买", notes = "下单购买")
    @GetMapping("/purchaseMovie")
    public AppV1Result purchaseMovie(String uuid, String userToken, String movieId){
        return movieV1Service.purchaseMovie(uuid, userToken, movieId);
    }

    @ApiOperation(value = "播放视频", notes = "播放视频")
    @GetMapping("/getPlayUrl")
    public AppV1Result getPlayUrl(String uuid, String userToken, String movieId,String keyWord,boolean needLogin){
        return movieV1Service.getPlayUrl(uuid, userToken, movieId,keyWord,needLogin);
    }



    @ApiOperation(value = "账号修改密码", notes = "账号修改密码")
    @GetMapping("/updatePwd")
    public AppV1Result updatePwd(String uuid,String userToken,String password,String newPassword)throws Exception{
        return movieV1Service.updatePwd(uuid, userToken, password, newPassword);

    }


    @ApiOperation(value = "流量充值列表", notes = "流量充值列表")
    @GetMapping("/flowDataList")
    public AppV1Result flowDataList(String keyWord,String uuid,String userToken){
        return movieV1Service.flowDataList(keyWord,uuid,userToken);
    }

    @ApiOperation(value = "宽带流量列表", notes = "宽带流量列表")
    @GetMapping("/broadDataList")
    public AppV1Result broadDataList(String keyWord,String uuid,String userToken){
        return movieV1Service.broadDataList(keyWord,uuid,userToken);
    }

    @ApiOperation(value = "下单购买", notes = "下单购买")
    @GetMapping("/purchaseFlowOrBroadData")
    public AppV1Result purchaseFlowOrBroadData(String uuid, String userToken, String keyWord){
        return movieV1Service.purchaseFlowOrBroadData(uuid, userToken, keyWord);
    }

    @ApiOperation(value = "SailasTV", notes = "SailasTV")
    @GetMapping("/getSailasTVInfo")
    public AppV1Result getSailasTVInfo(String uuid, String userToken,boolean needLogin,String androidType){
        return movieV1Service.getSailasTVInfo(uuid, userToken, needLogin,androidType);
    }

}
